package com.kerrykidz.authority.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.authority.dao.model.SecObj2usrEntity;
import com.kerrykidz.authority.dao.model.SecRoleEntity;
import com.kerrykidz.authority.dao.model.SecUsrEntity;
import com.kerrykidz.authority.service.IAuRoleService;
import com.kerrykidz.authority.service.IAuUserService;
import com.kerrykidz.common.Constants;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.system.dao.model.SysParaEntity;
import com.kerrykidz.system.util.UserUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.io.JsonUtil;
/***
 * 
 * @ClassName: AuUserController 
 * @description: 用户管理  sec_usr
 * @author: wolf-yansl 
 * @date: 2014年9月16日 下午3:52:04 
 * @version: V1.0
 *
 */
@Controller
@RequestMapping("/authority/user")
public class AuUserController {
	

	@Autowired
	private IAuRoleService roleService;
	@Autowired
	private IAuUserService userService;
	@Autowired
	private ICommonService commonService;
	
	@RequestMapping("home")
	public ModelAndView home() throws RDPException{
		ModelAndView model= new ModelAndView("authority/user_list");
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		return model;
	}
	
	/**
	 * 模糊搜索
	 * @param user
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月18日
	 */
	@RequestMapping("seacheLikeUser")
	@ResponseBody
	public SecUsrEntity seacheLikeUser(SecUsrEntity user) throws RDPException{
		user.setIspaging(true);
		user.setModiBy(UserUtil.getUserFromSession().getUserId());
		List<SecUsrEntity> userList = userService.findLikeUser(user);
		user.setResults(userList);
		return user;
	}
	/***
	 * 检测是否存在
	 * @param loginId
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月19日
	 */
	@RequestMapping("seacheByUser")
	@ResponseBody
	public SecUsrEntity seacheByUser(SecUsrEntity user)throws RDPException{
		return userService.findByUserEntity(user);
	}
	
	/**
	 * 新建用户页面跳转
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月18日
	 */
	@RequestMapping("addUserPage")
	public ModelAndView addUserPage() throws RDPException{
		ModelAndView model= new ModelAndView("authority/user_add");
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("roleAll", roleService.findByRoleWhere(new SecRoleEntity()));
		model.addObject("branId", UserUtil.getUserFromSession().getBranId());
		return model;
	}
	
	/***
	 * 添加用户
	 * @param user
	 * @param session
	 * @throws RDPException
	 * @date: 2014年9月19日
	 */
	@RequestMapping("addUser")
	public void addUser(SecUsrEntity user) throws RDPException{
		user.setModiBy(UserUtil.getUserFromSession().getUserId());
		userService.savetUser(user);
	}
	
	/**
	 *  修改用户  首页
	 * @param userId
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月19日
	 */
	@RequestMapping("editUserPage")
	public ModelAndView editUserPage(@RequestParam("userId")String userId ) throws RDPException{
		ModelAndView model= new ModelAndView("authority/user_edit");
		
		SecUsrEntity  user = userService.findByUserId(userId);
		List<SecObj2usrEntity> objList = userService.findByUserIdObjAll(userId);
		List<String> roleList = userService.findByUserIdRoleAll(userId);
		
		model.addObject("user", user);
		model.addObject("objList", JsonUtil.AllJsonUtil().toJson(objList));
		model.addObject("roleList", JsonUtil.AllJsonUtil().toJson(roleList));
		model.addObject("roleAll", roleService.findByRoleWhere(new SecRoleEntity()));
		model.addObject("branAll", userService.findGblSecBranAll());
		model.addObject("branId", user.getFkBranId());
		/*model.addObject("branId", UserUtil.getUserFromSession().getBranId());*/
		
		return model;
	}
	
	/**
	 * 修改用户
	 * @param user
	 * @throws RDPException
	 * @date: 2014年9月19日
	 */
	@RequestMapping("editUser")
	public void editUser(SecUsrEntity user)throws RDPException{
		user.setModiBy(UserUtil.getUserFromSession().getUserId());
		userService.updateUser(user);
	}
	
	/**
	 * 
	 * @param userId
	 * @throws RDPException 
	 * @date: 2014年9月19日
	 */
	@RequestMapping("deleteUser")
	public void deleteUser(@RequestParam("userId")String userId) throws RDPException{
		userService.deleteUser(userId,UserUtil.getUserFromSession().getUserId());
	}
	
	/**
	 * 重置密码
	 * @param userId
	 * @throws RDPException 
	 * @date: 2014年9月19日
	 */
	@RequestMapping("resetPass")
	@ResponseBody
	public int resetPass(@RequestParam("userId")String userId) throws RDPException{
		return userService.updateResetPass(userId,UserUtil.getUserFromSession().getUserId());
	}
	
	
	@RequestMapping(value = "/uploadFile")
	@ResponseBody
	public String uploadFile(
			@RequestParam(value = "file", required = false) MultipartFile file,HttpSession session)
			throws UnsupportedEncodingException, RDPException {
		SysParaEntity para = commonService
				.findSysParaById(Constants.SYS_BASE_PATH_IMAGE);
		String fileName =System.currentTimeMillis() + "";
		File targetFile = new File(para.getParaValue(), fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// 保存
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		fileName = URLEncoder.encode(fileName, "UTF-8");
		return fileName;
	}

	

}
